function debug1(str)
  if(false) then 
    toast(str)
  end 
end 


--查找返回按钮并点击
function findReturnBtn()
  x, y = findMultiColorInRegionFuzzy(0x0ec9e0,"24|2|0x0dcee3,46|11|0x1bb8d2,53|23|0x22a1bf,28|27|0x111829,-5|25|0x121829,-7|50|0x21a2db,23|46|0x1fc7ed,55|41|0x28c3eb", 80, 18, 4, 161, 93, 0, 0)
  if x > -1 then
    tap(x,y);
    return true
  end
  return false
end  
-- 点击命令
function tap(x, y)
  touchDown(1, x, y);
  mSleep(20);
  touchUp(1, x, y);
end

-- 进入冒险模式
function loadMaoXian()
  x, y = findMultiColorInRegionFuzzy(0x854d37,"16|9|0xa29e9e,38|14|0xdfdfcd,70|26|0x593838,89|13|0x928189,-1441|-751|0x121829,108|34|0x6a3f37,118|22|0x886160,132|27|0x492737", 80, 1498, 793, 1699, 850, 0, 0)
  if x > -1 then
    tap(x, y)
    return true
  end
  return false
end

-- 进入挑战模式
function loadTiaoZhan()
  x, y = findColorInRegionFuzzy(0x1f3d5e, 80, 808, 793, 1139, 889, 0, 0)
  if x > -1 then
    tap(x,y)
    return true
  end
  return false
end 

-- 挑战 地图上下移动
function tz_shang(val)
  if val==1 then 
    -- 上
    x, y = findMultiColorInRegionFuzzy(0x00c1ff,"-34|-5|0x011421,-13|-7|0x01344d,19|-8|0x01263c,45|14|0x01324a,55|1|0x011223,6|27|0x012130,-13|13|0x00c1ff,-9|-3|0x014c6b", 80, 411, 110, 589, 184, 0, 0)
    if x > -1 then
      tap(x,y)
      return true
    end
  else
    -- 下
    x, y = findMultiColorInRegionFuzzy(0x00c1ff,"-18|-17|0x00c1ff,-50|-12|0x01293c,-42|1|0x012740,41|0|0x022b45,2|-31|0x010f18,29|-24|0x00c1ff,56|-15|0x011a2b,-30|-23|0x00c1ff", 80, 410, 1003, 579, 1067, 0, 0)
    if x > -1 then
      tap(x,y)
      return true
    end
  end 
  return false
end 

-- 检测是否是挑战 页面
function checkTiaoZhan()
  x, y = findMultiColorInRegionFuzzy(0xfdfefe,"27|-13|0x124f80,38|-5|0xfefeff,39|16|0x126eb6,57|22|0x12568d,77|20|0x123d61,23|4|0x1270ba,20|33|0xfbfcfd,47|25|0xffffff", 80, 288, 5, 446, 88, 0, 0)
  if x > -1 then
    return true
  end
  return false
end 

-- 判断关卡是否完成
function tz_guan(val)   --1
  if val ==1 then 
    x, y = findMultiColorInRegionFuzzy(0xf3e6e6,"13|-14|0xe50a1a,24|26|0xd70914,17|23|0xece2e3", 81, 1346, 177, 1449, 295, 0, 0)
    if x > -1 then
      tap(x,y)
      return 1
    end
  elseif val ==2 then   --2
    x, y = findMultiColorInRegionFuzzy(0xfffefe,"26|-6|0xf00a1a,34|33|0xff0a23,27|25|0xede3e3", 81, 1343, 288, 1445, 390, 0, 0)
    if x > -1 then 
      tap(x,y)
      return 2
    end
  elseif val ==3 then   --3 
    x, y = findMultiColorInRegionFuzzy(0xfffefe,"-15|-10|0xbb050c,21|-11|0xe00a1a,30|34|0xfa0e27,23|19|0xf2e7e7", 81, 1339, 394, 1443, 493, 0, 0)
    if x > -1 then
      tap(x,y)
      return 3
    end
  elseif val ==4 then   --4 
    x, y = findMultiColorInRegionFuzzy(0xfdfafa,"24|-12|0xe80919,33|33|0xff0a23,26|28|0xfdf1fb", 81, 1350, 509, 1444, 607, 0, 0)
    if x > -1 then
      tap(x,y)
      return 4
    end
  end 
  return 0
end 

-- 点击挑战关卡
function tz_clickGuan(val )
  if val==1 then --1
    tap(1076,230)
  elseif val==2 then --2
    tap(1086,338)
  elseif val==3 then --3
    tap(1079,439)
  elseif val==4 then --4
    tap(1077,567)
  end 
end 

--挑战下一步
function tz_next()
  x, y = findMultiColorInRegionFuzzy(0x3ea1d6,"32|-8|0x3eb4de,45|17|0xe5f3fa,96|3|0x39acdb,89|36|0x3299d5,124|14|0xe9f5fb,151|20|0x358dce,166|44|0x327ec7,181|22|0x367fc4", 80, 1478, 923, 1733, 1017, 0, 0)
  if x > -1 then
    tap(x,y)
  end
end 

-- 挑战 闯关按钮
function tz_chuangGuan()
  x, y = findMultiColorInRegionFuzzy(0xffffff,"-63|-18|0xe39f2b,-4|-37|0xe5a630,18|-20|0xe6a42c,35|-2|0xfffdfb,47|-5|0xa6a39f,71|-6|0xe39a27,79|15|0xe3941f,10|20|0xe79e21", 80, 1335, 864, 1591, 961, 0, 0)
  if x > -1 then
    tap(x,y)
  end
end 

-- 挑战 开启自动战斗
function tz_auto()
  
  x, y = findMultiColorInRegionFuzzy(0xfcffff,"-5|-23|0x131618,21|20|0x2eb3dc,-9|21|0x2bb2e4,-18|34|0x131618,29|35|0x15181b", 70, 1755, 9, 1830, 76, 0, 0)
  if x > -1 then
    tap(x,y)
    return true;
  end
  return false
end 

-- 挑战 检查是否开启战斗状态
function tz_auto_state()
  x, y = findMultiColorInRegionFuzzy(0xf4fafb,"27|25|0x36ccfd,-10|26|0x35ceff,-16|38|0x1a5c9e,27|36|0x1a5c9e,6|-25|0x1a5c9e", 85, 1751, 3, 1834, 85, 0, 0)
  if x > -1 then    
    return true;
  end
  return false
end 

-- 挑战  是否是失败界面
function tz_shiBaiUi()
  x, y = findColor({0, 0, 1919, 1079}, 
    "873|213|0x979797,307|151|0x020408,1535|137|0x030508,1603|392|0x06080f,1078|402|0x2c6080,903|408|0x101b2d,226|431|0x070910,210|590|0x0d1320,526|570|0x294263,1153|576|0x2b4465,651|737|0x2bafc4,642|698|0xfc804f,1300|760|0x1d9ad4,1256|696|0x82fdfd,1071|981|0xe48d15,1000|977|0xffffff,428|985|0x050810,918|880|0x0d1e2a,1291|1035|0x050a10,1276|327|0x070a17",
    85, 0, 0, 0)
    if x > -1 then
      return true;
    end
    return false
  end 
  
  -- 挑战 失败按钮
  function tz_shiBai()
    x, y = findMultiColorInRegionFuzzy(0xfefefd,"91|0|0xe99e1a,67|7|0xffffff,83|34|0xe3911d,56|10|0xfaf8f7,104|5|0xe59a16", 81, 826, 938, 1118, 1021, 0, 0)
    if x > -1 then
      tap(x,y)
      return true;
    end
    return false
  end 
  
  -- 挑战 跳过剧情
  function tz_tiaoGuo()
    
    x, y = findMultiColorInRegionFuzzy(0xb1ceda,"-36|2|0x3b9ed9,-42|-11|0x43a9de,-6|-2|0xd6eff9,28|5|0x41a8dc,33|10|0xf7fcfd,41|13|0x3198d4,54|3|0xf5f8fa,68|6|0x3498d7", 80, 1690, 40, 1880, 94, 0, 0)
    if x > -1 then
      tap(x,y)
    end
  end 
  
  
  -- 领取符文
  function tz_fuWen()
    x, y = findMultiColorInRegionFuzzy(0xd9e3e8,"-31|-7|0xecbf54,21|-27|0xebba48,90|-18|0xe9b54d,97|10|0xecb64f,61|8|0xffffff", 81, 844, 907, 1083, 981, 0, 0)
    if x > -1 then
      tap(x,y)
    end
  end 
  
  
  -- 挑战结束 界面
  function tz_jieShu()
    x, y = findColor({0, 0, 1919, 1079}, 
      "936|90|0xfbf3c0,212|80|0x0a121a,740|113|0x121a3b,1792|93|0x08101a,176|555|0x394a8c,616|325|0x295294,836|341|0x4486d0,1846|391|0x0e162f,1546|517|0x21296b,743|571|0x1a2139,285|798|0x1f5082,403|957|0x162748,830|1016|0xffffff,1040|1013|0xffffff,1782|1017|0x050d1d,1225|1020|0x152647,1593|871|0x162f50,1603|707|0x1a244d,1176|450|0x3b54b7,657|793|0x192333",
      85, 0, 0, 0)
      if x > -1 then
        return true;
      end
      return false
    end 
    
    -- 返回挑战 界面按钮
    function tz_fanHui()
      x, y = findMultiColorInRegionFuzzy(0xe2e2e2,"28|-1|0x38a6d8,34|12|0x359fd6,44|19|0xafd7ed,61|7|0x1c4d67,34|-6|0x38abda,-47|-4|0x3aa4d8,-50|20|0x3490ce,24|4|0x36a0d4,-68|-29|0x45aedc", 80, 1031, 940, 1253, 1024, 0, 0)
      if x > -1 then
        tap(x,y)
        return true;
      end
      return false
    end 
    
    
    -- 点击屏幕继续
    function clickNext()
      x, y = findMultiColorInRegionFuzzy(0x90a9bd,"-27|2|0x3d5e74,-60|4|0x315a7d,-69|14|0x2a4660,-27|-6|0x446882,-10|0|0x92adc4,12|4|0x8eaac2,25|5|0x557079,59|6|0xf5f8fa", 80, 798, 975, 1153, 1060, 0, 0)
      if x > -1 then
        tap(x,y)
        return true;
      end
      return false
    end 
    
    -- 选择挑战等级 1 普通 ，2 精英,3.大师
    function tz_level(val)
      if val==1 then --1
        tap(1607,267)
      elseif val==2 then --2
        tap(1611,488)
      else --3
        tap(1610,702)
      end  
    end 
    
    
    -- 开始挑战
    function startTiaoZhan()
      
    end 
    
    
    -- 开始挑战
    function startTz()
      
      -- 检测是否是挑战页面不是则返回到主页面
      flag=checkTiaoZhan();
      mSleep(200);
			
			-- 检查是否是游戏主界面
			flagUI=false;
			uiConunt=0;
			while  not(flagUI) do
				-- 跳转到主页面
				count=0;
				while not(flag) do
					findReturnBtn();
					count=count+1;
					
					-- 进入冒险模式
					flag=loadMaoXian()
					flagUI=flag;
					if count>5 then
						break
					end 
					mSleep(500);
				end
				
				-- 判断是否正常
				if not(flag) then 
					toast("请先进入主游戏界面")
				end
				mSleep(2000);
			end 
			
      
      -- 进入挑战模式
      loadTiaoZhan();
      mSleep(200);
      
      -- 扫描为完成任务 
      -- 选择第一个图,第一个难度等级
      for tmpi=1,5 do
        tz_shang(1)
        mSleep(50);
      end
      tz_level(1);
      mSleep(50);
      
      -- 开始检测为完成的任务
      for i=1,4,1 do
        for j=1,3,1 do
          tz_level(j)
          mSleep(1000);
          replayCount=0
          for k=1,4,1 do
            tz_clickGuan(k)
            temp=tz_guan(k)
            if temp==k then
              -- if true then
              -- 下一步
              tz_next();
              mSleep(1000);
              
              -- 开始闯关
              tz_chuangGuan();
              mSleep(1000);
              
              -- 当成功开启自动战斗 值改为true  
              flag=false;
              while not(flag) do 
                debug1("检测自动战斗")
                flag=	tz_auto();
                mSleep(1000);
                flag=tz_auto_state();
                mSleep(1000);
              end 
              debug1("开启自动战斗")
              
              -- 判断是否结束
              flag=false;
              -- 记录是否成功
              while not(flag) do 
                playStatus= true
                flag=	tz_jieShu();
                if(flag==false) then
                  playStatus=false;
                  flag= tz_shiBaiUi();
                  if flag then 
                    flag=tz_shiBai();
                    debug1("失败返回")
                  end 
                else
                  playStatus= true
                  -- 点击继续
                  flag=clickNext();
                  debug1("点击继续")
                end
                mSleep(3000);
              end
              
              -- 
              tz_level(1)
              
              -- 当游戏成功时  
              if playStatus==true then 
                debug1("游戏闯关成功")
                mSleep(3000);
                -- 领取符文
                tz_fuWen();
                debug1("领取符文")
                mSleep(3000);
                
                -- 返回
                tz_fanHui();
                debug1("返回")
                mSleep(6000);
              end 
              -- 判断本次是否通过,没有则在次游戏
              if playStatus==false then 
                
                debug1("游戏闯关失败")
                mSleep(3000);
                k=k-1;
                -- 修改重复闯关次数
                replayCount=replayCount+1;
                -- 大于设置的闯关次数则退出
                if replayCount>3 then
                  toast("当前关卡已重复多次跳过本次关卡")
                  mSleep(3000);
                  break;
                end 
              end 
            else 
              --toast(string.format("已通过关卡或还不能闯关：%s",k))
            end 
            mSleep(500);
          end
        end
        tz_shang(2)
        mSleep(500);
      end
    end 
    
    
    
    
    function test() 
      
      -- if true then
      -- 下一步
      tz_next();
      mSleep(1000);
      
      -- 开始闯关
      tz_chuangGuan();
      mSleep(1000);
      
      -- 当成功开启自动战斗 值改为true  
      flag=false;
      while not(flag) do 
        debug1("检测自动战斗")
        flag=	tz_auto();
        mSleep(1000);
        flag=tz_auto_state();
        mSleep(1000);
      end 
      debug1("开启自动战斗")
      
      -- 判断是否结束
      flag=false;
      -- 记录是否成功
      while not(flag) do 
        
        playStatus= true
        flag=	tz_jieShu();
        if(flag==false) then
          playStatus=false;
          flag= tz_shiBaiUi();
          if flag then 
            flag=tz_shiBai();
            debug1("失败返回")
          end 
        else
          
          playStatus= true
          -- 点击继续
          flag=clickNext();
          debug1("点击继续")
        end
        mSleep(3000);
      end
      
      -- 
      tz_level(1)
      
      -- 当游戏成功时  
      if playStatus==true then 
        debug1("游戏闯关成功")
        mSleep(3000);
        -- 领取符文
        tz_fuWen();
        debug1("领取符文")
        mSleep(3000);
        
        
        tz_level(1)
        mSleep(1000);
        -- 返回
        tz_fanHui();
        debug1("返回")
        mSleep(6000);
      end 
      
      -- 判断本次是否通过,没有则在次游戏
      if playStatus==false then 
        
        debug1("游戏闯关失败")
        mSleep(3000);
        k=k-1;
        -- 修改重复闯关次数
        replayCount=replayCount+1;
        -- 大于设置的闯关次数则退出
        if replayCount>3 then
          toast("当前关卡已重复多次跳过本次关卡")
          mSleep(3000);
          
        end 
      end 
    end 
    
    init("0", 1); --以当前应用 Home 键在右边初始化
		setScreenScale(1080,1920)    --在540*960分辨率的手机中开发了脚本，要在720*1280的设备中运行 -
    yesorno,retuserui=showUI("ui.json");
		
		
    
		--  
    if(yesorno==1) then 
			q=1
			if(q==1) then
				startTz()
			else
				test()
			end 
    end 
    

    
    
    
    
    